package leetcode_801_900;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class LeeCode_899 {
    public static void main(String[] args) {
        System.out.println(orderlyQueue("baaca", 3));
        System.out.println(orderlyQueue("cba", 1));
        System.out.println(orderlyQueue("kuh", 1));
    }
    private static String orderlyQueue(String s, int k) {
        int n = s.length();
        if (k == 1){
            TreeSet<String> set = new TreeSet<>();
            for (int i = 0; i < n; i++) {
                set.add(s.substring(i) + s.substring(0, i));
            }
            return set.pollFirst();
        }else {
            char[] ans = new char[n];
            for (int i = 0; i < n; i++) {
                ans[i] = s.charAt(i);
            }
            Arrays.sort(ans);
            return new String(ans);
        }
    }
}
